import { JSEncrypt } from 'jsencrypt'
/**
 * @param {date} time 需要转换的时间
 * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
 */
export function formatTime(time, fmt) {
    if (!time) return '';
    else {
		//var offset = 	new Date().getTimezoneOffset() / 60 + 1;
		//console.log(offset);
		//var date = new Date(time*1000 + offset * 3600  * 1000);  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        //const date = new Date(time*1000-7*3600000);
		var date = new Date(time*1000);
        var year = date.getFullYear(),
        		  month = ("0" + (date.getMonth() + 1)).slice(-2),
        		  sdate = ("0" + date.getDate()).slice(-2),
        		  hour = ("0" + date.getHours()).slice(-2),
        		  minute = ("0" + date.getMinutes()).slice(-2),
        		  second = ("0" + date.getSeconds()).slice(-2);
        // 拼接
        var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
        // 返回
        return result;
    }
}

// i18n国际化页面标题
export function getPageTitle(i18n, key) {
    // const hasKey = i18n.te(`${key}`)
    const title = i18n.t(key)
    // if (hasKey) {
    //     return `${title}`
    // }
    return `${title}`
}

/**
 * 加密
 * @param {String}  需要加密的参数
 */
export function passwordEncryption (param) {
  // 后台给的公钥
  let publicKey = '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4HQ1kNOuTqaBoW6Fi0S6ACr8gBG+j3VGkNJFVHJK9qMfvlyzI7+81EtsrZWRK8mC1oY8MLXpZOkET5ItqoEHS7qENdxlWbFZzqsygVNbvSLgHm9AjwauugJ9NLvcY20qHdKideS0QGcUcaozVxni6sfddoiyRUcmBUVlezY2FCBI9+orM3SVWJthSCnI/ZAHpkybCNGf0R94lnuiUsFFuA+aP4dNYBd2KIypA/dfndUFuhd/1OfRFwoPR4L4j9oHs3d/NxnkQURys1vg4+yUxQeThkt0X5UJm4U3tMnWFzkyGWysVTk8/R4tEVohXmuZTUPlr3Ugfu3ECIpLlwj9GwIDAQAB-----END PUBLIC KEY-----'
  let encryptor = new JSEncrypt() // 新建JSEncrypt对象
  encryptor.setPublicKey(publicKey) // 设置公钥
  let passwordEncryp = encryptor.encrypt(param) // 对密码进行加密
  return passwordEncryp
}

/**
 * RSA 解密
 * @param data 需要解密的数据
 */
 export function rsaDecrypt(data) {
    //私钥，复制对应的私钥用于解密
    const PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
    MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDgdDWQ065OpoGh
    boWLRLoAKvyAEb6PdUaQ0kVUckr2ox++XLMjv7zUS2ytlZEryYLWhjwwtelk6QRP
    ki2qgQdLuoQ13GVZsVnOqzKBU1u9IuAeb0CPBq66An00u9xjbSod0qJ15LRAZxRx
    qjNXGeLqx912iLJFRyYFRWV7NjYUIEj36iszdJVYm2FIKcj9kAemTJsI0Z/RH3iW
    e6JSwUW4D5o/h01gF3YojKkD91+d1QW6F3/U59EXCg9HgviP2gezd383GeRBRHKz
    W+Dj7JTFB5OGS3RflQmbhTe0ydYXOTIZbKxVOTz9Hi0RWiFea5lNQ+WvdSB+7cQI
    ikuXCP0bAgMBAAECggEAVMINaD9HV1PxJ7DApTKyulWBPCt3Nl0m5JFCtPzj6fb3
    ib8ftqQ0/kXtKC+OvmY+DhxqpkpLdFu50QJ2VCidRK+ONXignRonu69SRxvfTLDa
    ySNh0aDIYl2GQl+efb0/ZjugNbzDhH5obfPDp2STF+2ZZ6V/DEItzVfDVp/GFBrX
    zXdY5lG/h1qJ8zp2QZeie07Ls6/MW9a5xDD3EgGMQXkG/rDS/hefPu1XvfHEtWoV
    PZflsTaybQA6GH+HfFqrnqw1QstCppMpPktjxnX6mA8pqcZ7ncD0GqP+nX3mTEk3
    ei8NvnO/sUOqcaRGGHaWb+Umrx0QdBtAm32EHZbswQKBgQD4HSOUuVas0UxxhKPm
    djokwlJipCAsCXHQKd/qxy3oJrEP/MzCshx9VnS0r2dbHaPWmS60xbI8fsv4EpLa
    uLSw6XHNfSgh+6tSFj0GZqqmzGS4fuDQ0o0cE4ReOuZCxKQTy/FYRU5MPOSQeRXj
    0TnsoA+QQKTh/XPz2dw67nt9wwKBgQDnlo3AqhY8BB8h75XXUJ2M9hZd8COcdRlj
    IPvtI6FJQwd6VVmpGlx74HyY5PP1CL+99zCBz3wMofrnU3UaSH8rPx8EiD6/Jkxf
    8m76vohNE3jB3KwqcA+laksmVuDt9Qiv35lznghWXM10fKPS/3hw0dPQ0QuVWBoP
    K9gmTtDVyQKBgGTBwI9G7LHllPuMd9rBLD99QmOYbxAzDasRNkNre3MJM2JgsO5n
    0jX8BAzUJk7ULJKbRmJW3li+zSoCsOxtkcM+Kd7e8otKXnpJWaLd0nNGEajyrW1f
    87nxGxIBLTz0CgWYDnxgOLmxCMi6w27uqm0zWt6jwdGcUZg3Yr3z1uZtAoGALUNe
    tz5HFDnV/+L8zVWDRY6/dmK+AxFcAeTreEDTgCcqqf1Ohd+SHeAVWii9ibHJEA7l
    Gyn7W9i42keUhquOt1wh6tfaM6//k20lxbpBl5brrjA8Mhsu46yvvXGTis6ugPlZ
    26ASjxaaTV8jAdVOq/ZGyPRiqDDM/xQZrfsPlAkCgYBxnXEUAxFKhTwNgvwYWMnk
    P5mL6wyAfJaJIYZCa9iTMN39+NJpHcGOqP4+DZcHOqfRLD6qOcIPDs0R7YZWf7cq
    KPBINAcKbPA/7us3i4SU0jWteUm1FW4fPTyBHykHBbnkxeu5xKjDH2QD8RGjJCwL
    FFjC5vfqWTB8TPd9s4Maqw==
    -----END PRIVATE KEY-----`
    //使用私钥解密
    const decrypt = new JSEncrypt()
    decrypt.setPrivateKey(PRIVATE_KEY)
    let result = decrypt.decrypt(data)
    return result
  }

